import global from '../../../utils/global'
import {
    addDispatch,
    getDispatchExamine,
    editDispatch,
    applyNewOccupation,
} from '../../api/long-insure'
const app = getApp()
Page({
    data: {
        dis: '',
        id: '',
        companyId: '',
        companyName: '',
        tempImg: '',
        videoUrl: [],
        companyArea: '',
        status: undefined,
        initial: undefined,
        videoDuration: 60,
        isWindows: false,
        limitSize: 100
    },
    async onLoad(options) {
        console.log(options, 'optionsoptionsoptions')
        this.setData({
            dis: options.dis,
            addType: options.addType || 'add',
        })
        if (options.addType === 'isfirstapply') {
            this.setData({
                companyId: options.id,
                companyName: options.name,
                companyArea: options.area,
            })
        } else {
            if (options.id !== 'null') {
                const result = await getDispatchExamine({
                    id: options.id,
                })
                console.log(result, 'resultresult')
                this.setData({
                    companyId: result.serveCompanyId,
                    companyName: result.serveCompanyName,
                    id: options.id,
                    status: result.status,
                },
                () => {
                    console.log(this.data.initial, '10000000')
                }
                )
                if (options?.addType !== 'apply') {
                    this.setData({
                        videoUrl: result.fileDetailList,
                        initial: result.fileDetailList.length - 1,
                    })
                }
            }
            if (options?.addType === 'apply') {
                wx.setNavigationBarTitle({
                    title: '申请新工种',
                })
            } else if (options?.addType === 'reapply') {
                wx.setNavigationBarTitle({
                    title: '重新申请',
                })
            }
        }
    },
    onSelect() {
        if ((this.data.dis === '1')) {
            return
        }
        if ((this.data.addType === 'apply') || (this.data.addType === 'reapply')) {
            return
        }
        wx.navigateTo({
            url: '/pages/enterprise/enterprise?select=1&radio=1',
            events: {
                complete: (data) => {
                    console.log(data, 'datadatadata')
                    if (data.length) {
                        this.setData({
                            // isInsuranceTime:true,
                            companyId: data[0].id,
                            companyName: data[0].name,
                            companyArea: data[0].area,
                        })
                    }
                },
            },
        })
    },
    async onVideo() {
        if (this.data.videoUrl.length >= 5) {
            app.toast('最多上传五条视频')
            return
        }
        // const video = await wx.chooseVideo({compressed:true})
        wx.chooseMedia({
            count: 1,
            mediaType: ['video'],
            sourceType: ['album', 'camera'],
            sizeType: ['compressed'],
            maxDuration: this.data.videoDuration,
            success: (res) => {
                if (res?.tempFiles[0]?.size > 1024 * 1024 * this.data.limitSize) {
                    app.toast(`视频大小不能超过${this.data.limitSize}M`)
                    return
                } else {
                    wx.showLoading({
                        title: '视频上传中',
                    })
                    wx.uploadFile({
                        url: global.baseUrl +
                        '/api/v1/pay/period-insur/serve-company/upload',
                        filePath: res.tempFiles[0].tempFilePath,
                        name: 'file',
                        success: (res) => {
                            res = JSON.parse(res.data)
                            if (res.code === 500) {
                                app.toast(res.msg)
                            } else {
                                const arr = []
                                arr.push(...this.data.videoUrl)
                                arr.push(res.data.fileUrl)
                                // tempImg: video.thumbTempFilePath,
                                this.setData({
                                    videoUrl: arr,
                                })
                            }
                            wx.hideLoading()
                        },
                        fail() {
                            wx.hideLoading({
                                success: () => {
                                    app.toast('文件上传失败')
                                },
                            })
                        },
                    })
                }
            },
            fail: async () => {
                let systemInfo = await wx.getSystemInfo()
                if (systemInfo.platform === 'windows') {
                    // app.toast('pc端小程序视频时间最长为10s')
                    this.setData({
                        isWindows: true,
                        videoDuration: 10,
                    })
                    setTimeout(() => {
                        this.onVideo()
                    }, 100)
                }
            },
        })
    },
    async onSubmit() {
        if (this.data.addType === 'apply') {
            await applyNewOccupation({
                periodServeCompanyId: this.data.id,
                params: {
                    videos: this.data.videoUrl,
                },
            })
        }else if (this.data.addType === 'isfirstapply') {
            await addDispatch({
                serveCompanyId: this.data.companyId,
                area: this.data.companyArea || '暂无',
                params: {
                    videos: this.data.videoUrl,
                },
            })
        } else {
            if (!this.data.id)
                await addDispatch({
                    serveCompanyId: this.data.companyId,
                    area: this.data.companyArea || '暂无',
                    params: {
                        videos: this.data.videoUrl,
                    },
                })
            else
                await editDispatch({
                    id: this.data.id,
                    serveCompanyId: this.data.companyId,
                    area: this.data.companyArea || '暂无',
                    params: {
                        videos: this.data.videoUrl,
                    },
                })
        }
        wx.redirectTo({
            url: '/packageLongInsure/pages/dispatch-success/dispatch-success',
        })
    },
    onVideoDelete(event) {
        // if (this.data.status === 1) {
        //     console.log(this.data.initial, 'initialinitialinitial')
        //     const { index } = event.currentTarget.dataset
        //     console.log(index, 'index')
        //     if (index > this.data.initial) {
        //         this.data.videoUrl.splice(index, 1)
        //         this.setData({
        //             videoUrl: this.data.videoUrl,
        //         })
        //     } else {
        //         app.toast('已通过的视频不可删除')
        //     }
        // } else {
        const {
            index
        } = event.currentTarget.dataset
        this.data.videoUrl.splice(index, 1)
        this.setData({
            videoUrl: this.data.videoUrl,
        })
        // }
    },
    /**
     * 点击查看视频
     * @param {*} ev
     */
    onWatchVideo(ev) {
        const dataset = ev.currentTarget.dataset
        const videos = [{
            url: this.data.videoUrl[dataset.index],
            type: 'video',
        }, ]
        wx.previewMedia({
            sources: videos,
        })
    }
})